package com.hutao.manage.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hutao.manage.enums.ExportDataEnum;
import com.hutao.manage.mapper.ExportCSVMapper;
import com.hutao.manage.query.ExportCSVParam;
import com.hutao.manage.service.ExportCSVService;
import com.hutao.manage.util.DateUtil;
@Service
public class ExportCSVServiceImpl implements ExportCSVService{
	
	
	
	
	@Autowired
	private ExportCSVMapper exportCSVMapper;

	
	//@Transactional(readOnly = true)
	@Override
	public List<Map<String, Object>> getExportDataInfo(ExportCSVParam exportCSVParam) {
		String deptCodes = exportCSVParam.getOutDepts();	
		List<Map<String,Object>> list = null;
		if(StringUtils.isNotBlank(deptCodes) && !"undefined".equals(deptCodes)) {
			exportCSVParam.setOutDeptList(Arrays.asList(deptCodes.split(",")));
		}
		
		
		//基本信息
		if(exportCSVParam.getDataType().intValue()==ExportDataEnum.PATIENT_INFO.getValue().intValue()) {
			list = exportCSVMapper.getPatientInfoList(exportCSVParam);			
			return list;
			//患者药物不良反应 (adr_info) 
		}else if(exportCSVParam.getDataType().intValue()==ExportDataEnum.ADR_INFO.getValue().intValue()) {
			list = exportCSVMapper.getAdrInfoList(exportCSVParam);		
			return list;
		//入院记录
		}else if(exportCSVParam.getDataType().intValue()==ExportDataEnum.EMR_ADMISSION.getValue().intValue()) {
			list = exportCSVMapper.getEmrAdmissionInfoList(exportCSVParam);	
			return list;
		//病程记录
		}else if(exportCSVParam.getDataType().intValue()==ExportDataEnum.EMR_PROCESS.getValue().intValue()) {
			list = exportCSVMapper.getEmrProcessInfoList(exportCSVParam);	
			return list;
		//出院记录
		}else if(exportCSVParam.getDataType().intValue()==ExportDataEnum.EMR_DISCHARGE.getValue().intValue()) {
			list = exportCSVMapper.getEmrDischargeProcessInfoList(exportCSVParam);	
			return list;
		//住院手术记录	
		}else if (exportCSVParam.getDataType().intValue()==ExportDataEnum.INPATIENT_SURGICAL_RECORD.getValue().intValue()) {
			list = exportCSVMapper.getInpatientSurgicalRecordList(exportCSVParam);	
			return list;
		//住院护理记录	
		}else if (exportCSVParam.getDataType().intValue()==ExportDataEnum.INPATIENT_NURSING_RECORD.getValue().intValue()) {
			list = exportCSVMapper.getInpatientNursingRecordList(exportCSVParam);	
			return list;
		//住院医嘱
		}else if (exportCSVParam.getDataType().intValue()==ExportDataEnum.INPATIENT_ORDER.getValue().intValue()) {
			list = exportCSVMapper.getInpatientOrderList(exportCSVParam);	
			return list;
		//住院收费记录
		}else if (exportCSVParam.getDataType().intValue()==ExportDataEnum.INPATIENT_EXPENSE_RECORD.getValue().intValue()) {
			list = exportCSVMapper.getInpatientExpenseRecordOrderList(exportCSVParam);	
			return list;
		//门诊信息记录	
		}else if (exportCSVParam.getDataType().intValue()==ExportDataEnum.OUTPATIENT_INFO.getValue().intValue()) {
			list = exportCSVMapper.getOutpatientInfoList(exportCSVParam);	
			return list;
		//门诊疾病及病历信息
		}else if (exportCSVParam.getDataType().intValue()==ExportDataEnum.OUTPATIENT_DIAG_MEDRECORD.getValue().intValue()) {
			list = exportCSVMapper.getOutpatientDiagMedrecordList(exportCSVParam);	
			return list;
		//门诊医嘱及处方
		}else if (exportCSVParam.getDataType().intValue()==ExportDataEnum.OUTPATIENT_ORDER_AND_PRES.getValue().intValue()) {
			list = exportCSVMapper.getOutpatientOrderAndPresList(exportCSVParam);	
			return list;
		//门诊费用信息	
		}else if (exportCSVParam.getDataType().intValue()==ExportDataEnum.OUTPATIENT_EXPENSE_RECORD.getValue().intValue()) {
			list = exportCSVMapper.getOutpatientExpenseRecordList(exportCSVParam);	
			return list;
		//临床检查信息
		}else if (exportCSVParam.getDataType().intValue()==ExportDataEnum.EXAM_INFO.getValue().intValue()) {
			list = exportCSVMapper.getExamInfoList(exportCSVParam);	
			return list;
		//实验室检查信息
		}else if (exportCSVParam.getDataType().intValue()==ExportDataEnum.LAB_INFO.getValue().intValue()) {
			list = exportCSVMapper.getLabInfoList(exportCSVParam);	
			return list;
		}else if (exportCSVParam.getDataType().intValue()==ExportDataEnum.PATHLOGICAL_EXAM_RECORD.getValue().intValue()) {
			list = exportCSVMapper.getPathlogicalExamRecordLabInfoList(exportCSVParam);	
			return list;
		//抗肿瘤药物采购清单
		}else if (exportCSVParam.getDataType().intValue()==ExportDataEnum.DRUG_PURCHASE.getValue().intValue()) {
			list = exportCSVMapper.getDrugPurchaseList(exportCSVParam);	
			return list;
		//病案首页
		}else if (exportCSVParam.getDataType().intValue()==ExportDataEnum.MEDICAL_RECORD_HOMEPAGE.getValue().intValue()) {
			list = exportCSVMapper.getMedicalHomepageList(exportCSVParam);	
			return list;
		}
		
		return null;
	}
	

	@Override
	public List<String> getMrOutLabOnlyId(ExportCSVParam exportCSVParam) {
		List<String> onlyIdList = new ArrayList<String>();
		List<String> mrOutOnlyIdList = exportCSVMapper.getMrOutLabOnlyId(exportCSVParam);
		List<String> mrInOnlyIdList = exportCSVMapper.getMrInLabOnlyId(exportCSVParam);
		onlyIdList.addAll(mrInOnlyIdList);
		onlyIdList.addAll(mrOutOnlyIdList);
		return onlyIdList;
	}

	@Override
	public List<Map<String, Object>> getMrOutLabRecordByOnlyIds(List<String> onlyIdList) {
		return exportCSVMapper.getMrOutLabRecordByOnlyIds(onlyIdList);
	}

	
	
	@Override
	public List<Map<String, Object>> getExportDrugUseDataInfo(ExportCSVParam exportCSVParam) {
		String drugTime = exportCSVParam.getStartTime();
		if(StringUtils.isEmpty(drugTime)) {
			return null;
		}
		String yearMonth = DateUtil.getYearMonthStr(drugTime);
		return exportCSVMapper.getDrugUserByMonth(yearMonth);
	}
	

}
